/*
 *   Author:         Kirin Zhong
 *   Created Date:   2024/6/3
 *
 *   Updated Date    Updated By      Reason of Update
 *   ------------    -----------     -----------------------------------------------
 */
package org.kirin.ocr.util;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.SecureRandom;
import java.util.Base64;
import java.nio.charset.StandardCharsets;
import java.security.Key;

public class CryptographyManager {

    private static final byte[] DEFAULT_BYTE = new byte[]{(byte) 220, (byte) 190, (byte) 86, (byte) 167, (byte) 195, (byte) 170, (byte) 223, (byte) 86, (byte) 210, (byte) 52, (byte) 193, (byte) 73, (byte) 160, (byte) 128, (byte) 132, (byte) 250};

    public static String encrypt(String value, String keyValue, boolean useDefaultByte) {
        byte b = 1;
        if (value == null || value.isEmpty()) return value;
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            Key key = new SecretKeySpec(keyValue.getBytes(StandardCharsets.UTF_8), "AES");
            IvParameterSpec iv = useDefaultByte ? new IvParameterSpec(DEFAULT_BYTE) : generateIv();
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
            byte[] encrypted = cipher.doFinal(value.getBytes());
            byte[] ivAndEncrypted = new byte[iv.getIV().length + encrypted.length];
            System.arraycopy(iv.getIV(), 0, ivAndEncrypted, 0, iv.getIV().length);
            System.arraycopy(encrypted, 0, ivAndEncrypted, iv.getIV().length, encrypted.length);
            return Base64.getEncoder().encodeToString(ivAndEncrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String decrypt(String value, String keyValue, boolean useDefaultByte) {
        if (value == null || value.isEmpty()) return value;
        try {
            byte[] ivAndEncrypted = Base64.getDecoder().decode(value);
            IvParameterSpec iv = useDefaultByte ? new IvParameterSpec(DEFAULT_BYTE) : new IvParameterSpec(ivAndEncrypted, 0, 16);
            byte[] encrypted = new byte[ivAndEncrypted.length - 16];
            System.arraycopy(ivAndEncrypted, 16, encrypted, 0, encrypted.length);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            Key key = new SecretKeySpec(keyValue.getBytes(StandardCharsets.UTF_8), "AES");
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            byte[] decrypted = cipher.doFinal(encrypted);
            return new String(decrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    private static IvParameterSpec generateIv() {
        // Generate a new IV for AES encryption
        byte[] iv = new byte[16];
        new SecureRandom().nextBytes(iv);
        return new IvParameterSpec(iv);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            String encryptCode = encrypt("011020", "12345678901234567890123456789012", true);
            System.out.println(encryptCode);
            System.out.println(decrypt(encryptCode, "12345678901234567890123456789012", true));
        }
    }
}
